import { changeAdminInfo, delAdmin, findAllAdmins } from "../../../../server/controllers/adminCon";
import api from "../../http/apis/apis"

export default {
    namespaced: true,
    state: {
        adminData:[],
        pageNumber: 1,
        total: 5,
        totalPages: 0,
        pageSize: 5,
    },
    mutations: {
        //数组数据接收
        setStateData(state, NewAdminData) {
            state.adminData = NewAdminData;
        },
        //修改页数
        setPageNumber(state,newPageNumber){
            state.pageNumber = newPageNumber;
        },
        //修改每页条数
        stePageSize(state,newPageSize){
            state.pageSize = newPageSize;
        }
    },
    actions: {
        //增加管理员
        async addAdmin(context, obj) {
            const { data } = await api.adminApi.addAccountApi(obj);
            // console.log(data);
            return data;

        },
        //获取管理员数据{pageSize : 5, pageNumber : 1}
        async findAllAdmins(context) {
            const { data } = await api.adminApi.getAdminListApi({
                pageSize:context.state.pageSize,
                pageNumber:context.state.pageNumber,
            });
            console.log('adminlist', data.data);
            // console.log('context', context);
            if (data.status) {
                const NewAdminData = data.data;
                context.commit("setStateData", NewAdminData);
            } else {
                this.$message.error(data.msg);
            }
        },
        //删除管理员
        async delAdmin(context, _id) {
            const { data } = await api.adminApi.delAdminApi(_id);
            context.dispatch("findAllAdmins");
            console.log('deldata', data);
        },
        //通过Id获取管理员
        async getAdminById(context, _id) {
            const { data } = await api.adminApi.getAdminByIdApi(_id);
            return data;
        },
        //修改管理员
        async changeAdmin(context, obj) {
            const { data } = await api.adminApi.upDataAdminApi(obj);
            return data;
        },

    },
    model: {
    }
}